package com.syni.mdd.yhd.common.all.listen.order;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.config.common.CommonSetUtils;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.entity.groupBuy.BmsUserBargain;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.enums.platformGood.PlatformGoodEnum;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.service.activity.BmsBackCardService;
import com.syni.mdd.yhd.common.all.service.activity.BmsDrLogicService;
import com.syni.mdd.yhd.common.all.service.activity.BmsDrOrderLogService;
import com.syni.mdd.yhd.common.all.service.groupBuy.BmsUserBargainService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class OrderCommitListener implements ApplicationListener<OrderEvent> {
    private final static String MODEL = "import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;";
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsUserBargainService bmsUserBargainService;

    @Autowired
    RedisTemplate redisTemplate;


    @Async
    public void onApplicationEvent(OrderEvent orderEvent) {
        String model = orderEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        Map<String,Object> userData = orderEvent.getUserDataMap();
        log.info("........................订单监听.......................");
        BmsDxOrder bmsDxOrder = orderEvent.getBmsDxOrder();
        if(bmsDxOrder.getOrderType() == Constants.GROUP_BUY_BARGAIN_ORDER_TYPE &&
                bmsDxOrder.getBargainId() != null){
            BmsUserBargain bmsUserBargain = bmsUserBargainService.getBargainById(bmsDxOrder.getBargainId());
            if(bmsUserBargain != null && StringUtils.isNotBlank(bmsUserBargain.getSaasOrderId())){
                bmsUserBargainService.orderCommit(bmsUserBargain.getSaasOrderId());
            }
        }else if(bmsDxOrder.getOrderType() == Constants.PLATFORM_ORDER_TYPE){
            postHandlePlatformOrder(bmsDxOrder,userData);
        }
    }

    private void postHandlePlatformOrder(BmsDxOrder bmsDxOrder, Map<String, Object> userData) {
        //处理占库存的
        String inVoredisKey = userData.get("platformNumRedisKey").toString();
        redisTemplate.opsForZSet().remove(Constants.REDIS_KEY_PLATFORM_GOOD_IN,inVoredisKey + ":" + bmsDxOrder.getPlatformGoodId());
        //下单了多少件
        redisTemplate.opsForZSet().incrementScore(Constants.REDIS_KEY_PLATFORM_ALREADY_BUY,"user:" + bmsDxOrder.getBmsUserId(),bmsDxOrder.getPlatformBuyNum());
        //设置今天购买
        Date nowTime = new Date();
        Integer startTime = DateTimeUtils.converDateToInterger(nowTime);
        Integer endTime = DateTimeUtils.converDateToInterger(DateTimeUtils.getEndTimeOfDay(nowTime));
        long restTime = (endTime - startTime);
        redisTemplate.opsForValue().setIfAbsent(Constants.REDIS_KEY_TODAY_BUY + bmsDxOrder.getBmsUserId() + ":" + bmsDxOrder.getPlatformGoodId(),1,restTime, TimeUnit.SECONDS);
    }
}
